package com.feiyi.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.R;
import com.feiyi.app.adapter.ClassifyLeftAdapter;

import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseFragment;
import com.feiyi.app.bean.ClassifyLeftBean;

import com.feiyi.app.net.HttpMethods;
import com.feiyi.app.utils.FragmentUtil;
import com.feiyi.app.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-14
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:分类界面
 * In me the tiger sniffs the rose
 */

public class ClassificationFragment extends BaseFragment {

    Unbinder unbinder;

    private View view;

    @BindView(R.id.rl_search)
    RelativeLayout rl_search;

   @BindView(R.id.rv_left)
    RecyclerView rv_left;


    /**
     * 左侧栏目数据
     */
   private List<ClassifyLeftBean.DataBean> leftList = new ArrayList<>();

    /**
     * 左侧频道列表适配器
     */
   private ClassifyLeftAdapter leftAdapter;

    /**
     * 右侧列表的fragment集合
     */
    private ArrayList<Fragment> fragments = new ArrayList<>();

    /**
     * 是否第一次初始化
     */
    private boolean firstInit = true;


    private static final String TAG = "ClassificationFragment";

    @Override
    public View getLayoutInflater(LayoutInflater inflater) {

        view = inflater.inflate(R.layout.fragment_classify,null);
        unbinder = ButterKnife.bind(this,view);

        return view;
    }

    @Override
    public void initView() {
        ImageUtil.suitDisplayWidth(rv_left,4);
    }

    @Override
    public void initDate() {

        HttpMethods.getInstance().info.getClassifyLeft(AppConstant.CLASSIFY_LEFT_URL).
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ClassifyLeftBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ClassifyLeftBean classifyLeftBean) {

                        if (null != classifyLeftBean.getData())
                            leftList.addAll(classifyLeftBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "onError: "+e.toString() );
                        ToastUtils.showShort("获取网络数据失败");
                    }

                    @Override
                    public void onComplete() {
                       makeDatas();
                       fillLeft();
                    }
                });
    }
    private void makeDatas(){

        for (int i = 0; i < leftList.size(); i++) {
            leftList.get(i).setSelect(false);
        }
        if (firstInit){
            leftList.get(0).setSelect(true);
        }
    }

    /**
     * 填充左半部分数据,并设置左边点击事件,设置点击第一个按钮
     */
    private void fillLeft(){

        leftAdapter.notifyDataSetChanged();
        rv_left.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                firstInit = false;
                makeDatas();
                leftList.get(position).setSelect(true);
                leftAdapter.notifyDataSetChanged();
                initRightFragment(position);
            }
        });
        initRightFragment(0);
    }

    private void initRightFragment(int position){

        fragments.clear();

        for (int i = 0; i < leftList.size(); i++) {
            ClassifyRightFragment rightFragment = new ClassifyRightFragment();
            Bundle bundle = new Bundle();
            bundle.putString("cat_id",leftList.get(position).getCat_id());
            rightFragment.setArguments(bundle);
            fragments.add(rightFragment);
        }
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fl_right_container,fragments.get(position));
        ft.commitNowAllowingStateLoss();
//        FragmentUtil.showFragment(activity,R.id.fl_right_container,fragments.get(position));
    }


    @Override
    public void initAdapter() {

        leftAdapter = new ClassifyLeftAdapter(R.layout.item_classify_left,leftList);
        rv_left.setLayoutManager(new LinearLayoutManager(activity));
        rv_left.addItemDecoration(new DividerItemDecoration(activity, LinearLayout.VERTICAL));
        rv_left.setAdapter(leftAdapter);



    }
}
